from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy       # 导入元素操作模块
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.touch_action import TouchAction        # 导入手势事件

# 实例化驱动
dict_p = {}
dict_p['deviceName'] = '127.0.0.1:62001'        # 可以通过adb devices -l 来获取
dict_p['platformName'] = 'Android'      # Android或者iOS
dict_p['platformVersion'] = '7.1.2'
dict_p['appPackage'] = 'com.lingyue.zebraloan'       # adb shell dumpsys activity activities
dict_p['appActivity'] = 'com.lingyue.banana.activities.BananaLaunchActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub',dict_p)
# 点击同意按钮
WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID,'com.lingyue.zebraloan:id/tv_confirm'))).click()
# d等待回到主页
sleep(10)
# 点击用户故事：索引 5
WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((MobileBy.CLASS_NAME,'android.widget.ImageView')))[5].click()
# 等待进入用户故事
sleep(7)
# 点击第一个故事 x 432 y 497
TouchAction(driver).tap(x=432,y=497).perform()
"""
所有的手势操作都是瞬发的，不享受元素等待时长
TouchAction: 手势操作
tap：点击事件，需要传入要点击的坐标值，x和y
perform：执行这一组动作
"""
# 等待4s
sleep(4)
# 向下滑动   x 470 y1508    x 450 y 414
TouchAction(driver).press(x=470,y=1508).move_to(x=450,y=414).release().perform()
"""
press：从哪里开始滑动
move_to：滑动到哪里
release()：松开双手
perform()：执行这一组动作
"""
# 点击返回按钮
WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((MobileBy.ID,'com.lingyue.zebraloan:id/tb_toolbar'))).click()
sleep(2)
# 获取文本断言
text = WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((MobileBy.ID,'com.lingyue.zebraloan:id/tv_title')))[-1].text
assert text == '3步借款 申请便捷'
sleep(10)